package com.nulldev.util.web.flexibleKit.flexRenderer.paths.subgens;

import com.nulldev.util.graphics.renderIt.api.renderItGraphicsInterface;
import com.nulldev.util.graphics.renderIt.api.core.RITFont;
import com.nulldev.util.graphics.renderIt.renderer.global.rColor;
import com.nulldev.util.graphics.renderIt.util.renderPosition;
import com.nulldev.util.gui.fontAPI.Font;
import com.nulldev.util.gui.fontAPI.FontManager;
import com.nulldev.util.web.flexibleKit.flexRenderer.FlexEnvironment;
import com.nulldev.util.web.flexibleKit.flexRenderer.paths.PathGenerator;
import com.nulldev.util.web.flexibleKit.general.RenderOperation;
import com.nulldev.util.web.flexibleKit.jfvParse.Node;

import static com.nulldev.util.web.flexibleKit.flexRenderer.paths.subgens.ParagraphGenerator.isBlankLine;

import com.nulldev.util.VariableAPI.ArrayUtils;

public class EMGenerator implements PathGenerator {

	@Override
	public RenderOperation generate(final Node n, final PathGenerationFlags... flags) {
		final RenderOperation ri = new RenderOperation() {

			private RITFont font;

			@Override
			public renderPosition doRender(final renderItGraphicsInterface i, final renderPosition rp, final FlexEnvironment env) {
				final int fontSize = i.viewport_getWidth() / 32;
				final String s = n.getTextContent();
				if (isBlankLine(s))
					return rp;
				final Font f = FontManager.gets("Product Sans", "Roboto");
				if (font == null) {
					font = i.awt_make_font(f.getFont(fontSize).deriveFont(java.awt.Font.ITALIC));
				}
				i.text_scalable(s, 5, rp.getY(), font, rColor.BLACK);
//				i.requestViewportHeight(rp.getY());
//				rp.appendGetY(fontSize);
//				i.runQuirkFix(RenderingQuirks.RESET_X_AFTER_CALL);
				return rp.clone().addX(fontSize * s.length());
			}

			@Override
			public Node operationFor() {
				return n;
			}

			@Override
			public boolean keepInline() {
				return ArrayUtils.contains(flags, PathGenerationFlags.RENDER_INLINE);
			}
		};
		return ri;
	}
}
